package BandB.Tool.GPhoneManager.AppManager.Grid;

import java.io.File;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;

import BandB.Tool.GPhoneManager.R;
import BandB.Tool.GPhoneManager.AppManager.PackInfoItem;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class UninstallerGridAppAdapter extends ArrayAdapter<ResolveInfo> {

	private LinkedList<PackInfoItem> mPackInfoList;
	private LayoutInflater mInflater;
	private PackageManager mPackageManager;
	private static final Collator sCollator = Collator.getInstance();

	public UninstallerGridAppAdapter(Context c) {
		super(c, 0);
		mPackInfoList = new LinkedList<PackInfoItem>();
		mInflater = LayoutInflater.from(c);
		mPackageManager = c.getPackageManager();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mPackInfoList.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.uninstaller_grid_item,
					null);
		}
		TextView name = (TextView) convertView
				.findViewById(R.id.uninstaller_name);
		ImageView icon = (ImageView) convertView
				.findViewById(R.id.uninstaller_icon);

		PackInfoItem info = getInfo(position);
		name.setText(info.getAppName());
		icon.setImageDrawable(info.getIcon());
		return convertView;
	}

	public PackInfoItem getInfo(int position) {
		return mPackInfoList.get(position);
	}

	public void deleteInfo(int position) {
		mPackInfoList.remove(position);
	}

	public void addQueueData(Queue<ResolveInfo> queue) {
		ResolveInfo info = queue.poll();
		while (null != info) {
			ApplicationInfo appInfo = info.activityInfo.applicationInfo;
			File file = getSourceFile(appInfo);
			if (null != file) {
				long fileLength = file.length();
				long fileDate = file.lastModified();
				PackInfoItem item = new PackInfoItem();
				item.setResolveInfo(info);
				item.setAppName(info.loadLabel(mPackageManager).toString());
				item.setIcon(info.loadIcon(mPackageManager));
				item.setSizeLong(fileLength);
				item.setDateLong(fileDate);
				mPackInfoList.add(item);
			}
			info = queue.poll();
		}
	}

	/**
	 * Sort this adapter by size in UI thread.
	 */
	public void sortBySize() {
		Collections.sort(mPackInfoList, new Comparator<PackInfoItem>() {

			@Override
			public int compare(PackInfoItem object1, PackInfoItem object2) {
				// TODO Auto-generated method stub
				return object1.getSizeLong().compareTo(object2.getSizeLong());
			}
		});
	}

	/**
	 * Sort this adapter by name in UI thread.
	 */
	public void sortByName() {
		Collections.sort(mPackInfoList, new Comparator<PackInfoItem>() {

			@Override
			public int compare(PackInfoItem object1, PackInfoItem object2) {
				// TODO Auto-generated method stub

				return sCollator.compare(object1.getAppName(), object2
						.getAppName());
			}
		});
	}

	/**
	 * Sort this adapter by date in UI thread.
	 */
	public void sortByDate() {
		Collections.sort(mPackInfoList, new Comparator<PackInfoItem>() {
			@Override
			public int compare(PackInfoItem object1, PackInfoItem object2) {
				// TODO Auto-generated method stub
				return object1.getDateLong().compareTo(object2.getDateLong());
			}
		});
	}

	private File getSourceFile(ApplicationInfo applicationInfo) {
		String path = applicationInfo.sourceDir;
		File file = new File(path);
		if (file.exists()) {
			return file;
		} else {
			return null;
		}
	}

}
